home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1993 July / InfoMagic USENET CD-ROM July 1993.ISO / sources / unix / volume21 / rh2 / part02 < prev    next >
Encoding:
Internet Message Format  |  1990-03-21  |  33.1 KB

  1. Subject:  v21i007:  Find files using C expressions, Part02/02
  2. Newsgroups: comp.sources.unix
  3. Sender: sources
  4. Approved: rsalz@uunet.UU.NET
  5.  
  6. Submitted-by: Kenneth Stauffer <cpsc.UCalgary.CA!stauffer>
  7. Posting-number: Volume 21, Issue 7
  8. Archive-name: rh2/part02
  9.  
  10. #! /bin/sh
  11. # This is a shell archive.  Remove anything before this line, then unpack
  12. # it by saving it into a file and typing "sh file".  To overwrite existing
  13. # files, type "sh file -c".  You can also feed this as standard input via
  14. # unshar, or by typing "sh <file", e.g..  If this archive is complete, you
  15. # will see the following message at the end:
  16. #        "End of archive 2 (of 2)."
  17. # Contents:  getopt.c rhparse.c
  18. # Wrapped by rsalz@litchi.bbn.com on Wed Feb  7 15:35:30 1990
  19. PATH=/bin:/usr/bin:/usr/ucb ; export PATH
  20. if test -f 'getopt.c' -a "${1}" != "-c" ; then 
  21.   echo shar: Will not clobber existing file \"'getopt.c'\"
  22. else
  23. echo shar: Extracting \"'getopt.c'\" \(16259 characters\)
  24. sed "s/^X//" >'getopt.c' <<'END_OF_FILE'
  25. X
  26. X#undef _POSIX_OPTION_ORDER
  27. X
  28. X/* Getopt for GNU.
  29. X   Modified by David MacKenzie to use malloc and free instead of alloca,
  30. X   and memcpy instead of bcopy under System V.
  31. X   Copyright (C) 1987 Free Software Foundation, Inc.
  32. X
  33. X               NO WARRANTY
  34. X
  35. X  BECAUSE THIS PROGRAM IS LICENSED FREE OF CHARGE, WE PROVIDE ABSOLUTELY
  36. XNO WARRANTY, TO THE EXTENT PERMITTED BY APPLICABLE STATE LAW.  EXCEPT
  37. XWHEN OTHERWISE STATED IN WRITING, FREE SOFTWARE FOUNDATION, INC,
  38. XRICHARD M. STALLMAN AND/OR OTHER PARTIES PROVIDE THIS PROGRAM "AS IS"
  39. XWITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING,
  40. XBUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
  41. XFITNESS FOR A PARTICULAR PURPOSE.  THE ENTIRE RISK AS TO THE QUALITY
  42. XAND PERFORMANCE OF THE PROGRAM IS WITH YOU.  SHOULD THE PROGRAM PROVE
  43. XDEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR OR
  44. XCORRECTION.
  45. X
  46. X IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW WILL RICHARD M.
  47. XSTALLMAN, THE FREE SOFTWARE FOUNDATION, INC., AND/OR ANY OTHER PARTY
  48. XWHO MAY MODIFY AND REDISTRIBUTE THIS PROGRAM AS PERMITTED BELOW, BE
  49. XLIABLE TO YOU FOR DAMAGES, INCLUDING ANY LOST PROFITS, LOST MONIES, OR
  50. XOTHER SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE
  51. XUSE OR INABILITY TO USE (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR
  52. XDATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY THIRD PARTIES OR
  53. XA FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS) THIS
  54. XPROGRAM, EVEN IF YOU HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH
  55. XDAMAGES, OR FOR ANY CLAIM BY ANY OTHER PARTY.
  56. X
  57. X        GENERAL PUBLIC LICENSE TO COPY
  58. X
  59. X  1. You may copy and distribute verbatim copies of this source file
  60. Xas you receive it, in any medium, provided that you conspicuously and
  61. Xappropriately publish on each copy a valid copyright notice "Copyright
  62. X (C) 1987 Free Software Foundation, Inc."; and include following the
  63. Xcopyright notice a verbatim copy of the above disclaimer of warranty
  64. Xand of this License.  You may charge a distribution fee for the
  65. Xphysical act of transferring a copy.
  66. X
  67. X  2. You may modify your copy or copies of this source file or
  68. Xany portion of it, and copy and distribute such modifications under
  69. Xthe terms of Paragraph 1 above, provided that you also do the following:
  70. X
  71. X    a) cause the modified files to carry prominent notices stating
  72. X    that you changed the files and the date of any change; and
  73. X
  74. X    b) cause the whole of any work that you distribute or publish,
  75. X    that in whole or in part contains or is a derivative of this
  76. X    program or any part thereof, to be licensed at no charge to all
  77. X    third parties on terms identical to those contained in this
  78. X    License Agreement (except that you may choose to grant more
  79. X    extensive warranty protection to third parties, at your option).
  80. X
  81. X    c) You may charge a distribution fee for the physical act of
  82. X    transferring a copy, and you may at your option offer warranty
  83. X    protection in exchange for a fee.
  84. X
  85. X  3. You may copy and distribute this program or any portion of it in
  86. Xcompiled, executable or object code form under the terms of Paragraphs
  87. X1 and 2 above provided that you do the following:
  88. X
  89. X    a) cause each such copy to be accompanied by the
  90. X    corresponding machine-readable source code, which must
  91. X    be distributed under the terms of Paragraphs 1 and 2 above; or,
  92. X
  93. X    b) cause each such copy to be accompanied by a
  94. X    written offer, with no time limit, to give any third party
  95. X    free (except for a nominal shipping charge) a machine readable
  96. X    copy of the corresponding source code, to be distributed
  97. X    under the terms of Paragraphs 1 and 2 above; or,
  98. X
  99. X    c) in the case of a recipient of this program in compiled, executable
  100. X    or object code form (without the corresponding source code) you
  101. X    shall cause copies you distribute to be accompanied by a copy
  102. X    of the written offer of source code which you received along
  103. X    with the copy you received.
  104. X
  105. X  4. You may not copy, sublicense, distribute or transfer this program
  106. Xexcept as expressly provided under this License Agreement.  Any attempt
  107. Xotherwise to copy, sublicense, distribute or transfer this program is void and
  108. Xyour rights to use the program under this License agreement shall be
  109. Xautomatically terminated.  However, parties who have received computer
  110. Xsoftware programs from you with this License Agreement will not have
  111. Xtheir licenses terminated so long as such parties remain in full compliance.
  112. X
  113. X  5. If you wish to incorporate parts of this program into other free
  114. Xprograms whose distribution conditions are different, write to the Free
  115. XSoftware Foundation at 675 Mass Ave, Cambridge, MA 02139.  We have not yet
  116. Xworked out a simple rule that can be stated here, but we will often permit
  117. Xthis.  We will be guided by the two goals of preserving the free status of
  118. Xall derivatives of our free software and of promoting the sharing and reuse of
  119. Xsoftware.
  120. X
  121. X
  122. XIn other words, you are welcome to use, share and improve this program.
  123. XYou are forbidden to forbid anyone else to use, share and improve
  124. Xwhat you give them.   Help stamp out software-hoarding!  */
  125. X
  126. X/* This version of `getopt' appears to the caller like standard Unix `getopt'
  127. X   but it behaves differently for the user, since it allows the user
  128. X   to intersperse the options with the other arguments.
  129. X
  130. X   As `getopt' works, it permutes the elements of `argv' so that,
  131. X   when it is done, all the options precede everything else.  Thus
  132. X   all application programs are extended to handle flexible argument order.
  133. X
  134. X   Setting the environment variable _POSIX_OPTION_ORDER disables permutation.
  135. X   Then the behavior is completely standard.
  136. X
  137. X   GNU application programs can use a third alternative mode in which
  138. X   they can distinguish the relative order of options and other arguments.  */
  139. X
  140. X#include <stdio.h>
  141. X
  142. X#if SYSV || SYSVR3
  143. X#define bcopy(s, d, l) memcpy((d), (s), (l))
  144. X#define index strchr
  145. X#endif
  146. X
  147. X/* For communication from `getopt' to the caller.
  148. X   When `getopt' finds an option that takes an argument,
  149. X   the argument value is returned here.
  150. X   Also, when `ordering' is RETURN_IN_ORDER,
  151. X   each non-option ARGV-element is returned here.  */
  152. X
  153. Xchar *optarg = 0;
  154. X
  155. X/* Index in ARGV of the next element to be scanned.
  156. X   This is used for communication to and from the caller
  157. X   and for communication between successive calls to `getopt'.
  158. X
  159. X   On entry to `getopt', zero means this is the first call; initialize.
  160. X
  161. X   When `getopt' returns EOF, this is the index of the first of the
  162. X   non-option elements that the caller should itself scan.
  163. X
  164. X   Otherwise, `optind' communicates from one call to the next
  165. X   how much of ARGV has been scanned so far.  */
  166. X
  167. Xint optind = 0;
  168. X
  169. X/* The next char to be scanned in the option-element
  170. X   in which the last option character we returned was found.
  171. X   This allows us to pick up the scan where we left off.
  172. X
  173. X   If this is zero, or a null string, it means resume the scan
  174. X   by advancing to the next ARGV-element.  */
  175. X
  176. Xstatic char *nextchar;
  177. X
  178. X/* Callers store zero here to inhibit the error message
  179. X   for unrecognized options.  */
  180. X
  181. Xint opterr = 1;
  182. X
  183. X/* Describe how to deal with options that follow non-option ARGV-elements.
  184. X
  185. X   UNSPECIFIED means the caller did not specify anything;
  186. X   the default is then REQUIRE_ORDER if the environment variable
  187. X   _OPTIONS_FIRST is defined, PERMUTE otherwise.
  188. X
  189. X   REQUIRE_ORDER means don't recognize them as options.
  190. X   Stop option processing when the first non-option is seen.
  191. X   This is what Unix does.
  192. X
  193. X   PERMUTE is the default.  We permute the contents of `argv' as we scan,
  194. X   so that eventually all the options are at the end.  This allows options
  195. X   to be given in any order, even with programs that were not written to
  196. X   expect this.
  197. X
  198. X   RETURN_IN_ORDER is an option available to programs that were written
  199. X   to expect options and other ARGV-elements in any order and that care about
  200. X   the ordering of the two.  We describe each non-option ARGV-element
  201. X   as if it were the argument of an option with character code zero.
  202. X   Using `-' as the first character of the list of option characters
  203. X   requests this mode of operation.
  204. X
  205. X   The special argument `--' forces an end of option-scanning regardless
  206. X   of the value of `ordering'.  In the case of RETURN_IN_ORDER, only
  207. X   `--' can cause `getopt' to return EOF with `optind' != ARGC.  */
  208. X
  209. Xstatic enum { REQUIRE_ORDER, PERMUTE, RETURN_IN_ORDER } ordering;
  210. X
  211. Xchar *malloc ();
  212. X
  213. X/* Allocate memory dynamically, with error checking.  */
  214. X
  215. Xstatic char *
  216. Xxmalloc (n)
  217. X  unsigned n;
  218. X{
  219. X  char *p;
  220. X
  221. X  p = malloc (n);
  222. X  if (!p)
  223. X    {
  224. X      fprintf (stderr, "Virtual memory exhausted\n");
  225. X      exit (1);
  226. X    }
  227. X  return p;
  228. X}
  229. X
  230. X/* Handle permutation of arguments.  */
  231. X
  232. X/* Describe the part of ARGV that contains non-options that have
  233. X   been skipped.  `first_nonopt' is the index in ARGV of the first of them;
  234. X   `last_nonopt' is the index after the last of them.  */
  235. X
  236. Xstatic int first_nonopt;
  237. Xstatic int last_nonopt;
  238. X
  239. X/* Exchange two adjacent subsequences of ARGV.
  240. X   One subsequence is elements [first_nonopt,last_nonopt)
  241. X    which contains all the non-options that have been skipped so far.
  242. X   The other is elements [last_nonopt,optind), which contains all
  243. X    the options processed since those non-options were skipped.
  244. X
  245. X   `first_nonopt' and `last_nonopt' are relocated so that they describe
  246. X    the new indices of the non-options in ARGV after they are moved.  */
  247. X
  248. Xstatic void
  249. Xexchange (argv)
  250. X     char **argv;
  251. X{
  252. X  int nonopts_size
  253. X    = (last_nonopt - first_nonopt) * sizeof (char *);
  254. X  char **temp = (char **) xmalloc (nonopts_size);
  255. X
  256. X  /* Interchange the two blocks of data in argv.  */
  257. X
  258. X  bcopy (&argv[first_nonopt], temp, nonopts_size);
  259. X  bcopy (&argv[last_nonopt], &argv[first_nonopt],
  260. X     (optind - last_nonopt) * sizeof (char *));
  261. X  bcopy (temp, &argv[first_nonopt + optind - last_nonopt],
  262. X     nonopts_size);
  263. X
  264. X  free (temp);
  265. X
  266. X  /* Update records for the slots the non-options now occupy.  */
  267. X
  268. X  first_nonopt += (optind - last_nonopt);
  269. X  last_nonopt = optind;
  270. X}
  271. X
  272. X/* Scan elements of ARGV (whose length is ARGC) for option characters
  273. X   given in OPTSTRING.
  274. X
  275. X   If an element of ARGV starts with '-', and is not exactly "-" or "--",
  276. X   then it is an option element.  The characters of this element
  277. X   (aside from the initial '-') are option characters.  If `getopt'
  278. X   is called repeatedly, it returns successively each of the option characters
  279. X   from each of the option elements.
  280. X
  281. X   If `getopt' finds another option character, it returns that character,
  282. X   updating `optind' and `nextchar' so that the next call to `getopt' can
  283. X   resume the scan with the following option character or ARGV-element.
  284. X
  285. X   If there are no more option characters, `getopt' returns `EOF'.
  286. X   Then `optind' is the index in ARGV of the first ARGV-element
  287. X   that is not an option.  (The ARGV-elements have been permuted
  288. X   so that those that are not options now come last.)
  289. X
  290. X   OPTSTRING is a string containing the legitimate option characters.
  291. X   A colon in OPTSTRING means that the previous character is an option
  292. X   that wants an argument.  The argument is taken from the rest of the
  293. X   current ARGV-element, or from the following ARGV-element,
  294. X   and returned in `optarg'.
  295. X
  296. X   If an option character is seen that is not listed in OPTSTRING,
  297. X   return '?' after printing an error message.  If you set `opterr' to
  298. X   zero, the error message is suppressed but we still return '?'.
  299. X
  300. X   If a char in OPTSTRING is followed by a colon, that means it wants an arg,
  301. X   so the following text in the same ARGV-element, or the text of the following
  302. X   ARGV-element, is returned in `optarg.  Two colons mean an option that
  303. X   wants an optional arg; if there is text in the current ARGV-element,
  304. X   it is returned in `optarg'.
  305. X
  306. X   If OPTSTRING starts with `-', it requests a different method of handling the
  307. X   non-option ARGV-elements.  See the comments about RETURN_IN_ORDER, above.
  308. X */
  309. X
  310. Xint
  311. Xgetopt (argc, argv, optstring)
  312. X     int argc;
  313. X     char **argv;
  314. X     char *optstring;
  315. X{
  316. X  /* Initialize the internal data when the first call is made.
  317. X     Start processing options with ARGV-element 1 (since ARGV-element 0
  318. X     is the program name); the sequence of previously skipped
  319. X     non-option ARGV-elements is empty.  */
  320. X
  321. X  if (optind == 0)
  322. X    {
  323. X      first_nonopt = last_nonopt = optind = 1;
  324. X
  325. X      nextchar = 0;
  326. X
  327. X      /* Determine how to handle the ordering of options and nonoptions.  */
  328. X
  329. X      if (optstring[0] == '-')
  330. X    ordering = RETURN_IN_ORDER;
  331. X      else if (getenv ("_POSIX_OPTION_ORDER") != 0)
  332. X    ordering = REQUIRE_ORDER;
  333. X      else
  334. X    ordering = PERMUTE;
  335. X    }
  336. X
  337. X  if (nextchar == 0 || *nextchar == 0)
  338. X    {
  339. X      if (ordering == PERMUTE)
  340. X    {
  341. X      /* If we have just processed some options following some non-options,
  342. X         exchange them so that the options come first.  */
  343. X
  344. X      if (first_nonopt != last_nonopt && last_nonopt != optind)
  345. X        exchange (argv);
  346. X      else if (last_nonopt != optind)
  347. X        first_nonopt = optind;
  348. X
  349. X      /* Now skip any additional non-options
  350. X         and extend the range of non-options previously skipped.  */
  351. X
  352. X      while (optind < argc
  353. X         && (argv[optind][0] != '-'
  354. X             || argv[optind][1] == 0))
  355. X        optind++;
  356. X      last_nonopt = optind;
  357. X    }
  358. X
  359. X      /* Special ARGV-element `--' means premature end of options.
  360. X     Skip it like a null option,
  361. X     then exchange with previous non-options as if it were an option,
  362. X     then skip everything else like a non-option.  */
  363. X
  364. X      if (optind != argc && !strcmp (argv[optind], "--"))
  365. X    {
  366. X      optind++;
  367. X
  368. X      if (first_nonopt != last_nonopt && last_nonopt != optind)
  369. X        exchange (argv);
  370. X      else if (first_nonopt == last_nonopt)
  371. X        first_nonopt = optind;
  372. X      last_nonopt = argc;
  373. X
  374. X      optind = argc;
  375. X    }
  376. X
  377. X      /* If we have done all the ARGV-elements, stop the scan
  378. X     and back over any non-options that we skipped and permuted.  */
  379. X
  380. X      if (optind == argc)
  381. X    {
  382. X      /* Set the next-arg-index to point at the non-options
  383. X         that we previously skipped, so the caller will digest them.  */
  384. X      if (first_nonopt != last_nonopt)
  385. X        optind = first_nonopt;
  386. X      return EOF;
  387. X    }
  388. X
  389. X      /* If we have come to a non-option and did not permute it,
  390. X     either stop the scan or describe it to the caller and pass it by.  */
  391. X
  392. X      if (argv[optind][0] != '-' || argv[optind][1] == 0)
  393. X    {
  394. X      if (ordering == REQUIRE_ORDER)
  395. X        return EOF;
  396. X      optarg = argv[optind++];
  397. X      return 0;
  398. X    }
  399. X
  400. X      /* We have found another option-ARGV-element.
  401. X     Start decoding its characters.  */
  402. X
  403. X      nextchar = argv[optind] + 1;
  404. X    }
  405. X
  406. X  /* Look at and handle the next option-character.  */
  407. X
  408. X  {
  409. X    char c = *nextchar++;
  410. X    char *temp = (char *) index (optstring, c);
  411. X
  412. X    /* Increment `optind' when we start to process its last character.  */
  413. X    if (*nextchar == 0)
  414. X      optind++;
  415. X
  416. X    if (temp == 0 || c == ':')
  417. X      {
  418. X    if (opterr != 0)
  419. X      {
  420. X        if (c < 040 || c >= 0177)
  421. X          fprintf (stderr, "%s: unrecognized option, character code 0%o\n",
  422. X               argv[0], c);
  423. X        else
  424. X          fprintf (stderr, "%s: unrecognized option `-%c'\n",
  425. X               argv[0], c);
  426. X      }
  427. X    return '?';
  428. X      }
  429. X    if (temp[1] == ':')
  430. X      {
  431. X    if (temp[2] == ':')
  432. X      {
  433. X        /* This is an option that accepts an argument optionally.  */
  434. X        if (*nextchar != 0)
  435. X          {
  436. X            optarg = nextchar;
  437. X        optind++;
  438. X          }
  439. X        else
  440. X          optarg = 0;
  441. X        nextchar = 0;
  442. X      }
  443. X    else
  444. X      {
  445. X        /* This is an option that requires an argument.  */
  446. X        if (*nextchar != 0)
  447. X          {
  448. X        optarg = nextchar;
  449. X        /* If we end this ARGV-element by taking the rest as an arg,
  450. X           we must advance to the next element now.  */
  451. X        optind++;
  452. X          }
  453. X        else if (optind == argc)
  454. X          {
  455. X        if (opterr != 0)
  456. X          fprintf (stderr, "%s: no argument for `-%c' option\n",
  457. X               argv[0], c);
  458. X        c = '?';
  459. X          }
  460. X        else
  461. X          /* We already incremented `optind' once;
  462. X         increment it again when taking next ARGV-elt as argument.  */
  463. X          optarg = argv[optind++];
  464. X        nextchar = 0;
  465. X      }
  466. X      }
  467. X    return c;
  468. X  }
  469. X}
  470. X
  471. Xmemcpy(s,d,len)
  472. Xchar *s,*d;
  473. Xint len;
  474. X{
  475. X    while(len--) *d++ = *s++;
  476. X}
  477. END_OF_FILE
  478. if test 16259 -ne `wc -c <'getopt.c'`; then
  479.     echo shar: \"'getopt.c'\" unpacked with wrong size!
  480. fi
  481. # end of 'getopt.c'
  482. fi
  483. if test -f 'rhparse.c' -a "${1}" != "-c" ; then 
  484.   echo shar: Will not clobber existing file \"'rhparse.c'\"
  485. else
  486. echo shar: Extracting \"'rhparse.c'\" \(14503 characters\)
  487. sed "s/^X//" >'rhparse.c' <<'END_OF_FILE'
  488. X
  489. X/* ----------------------------------------------------------------------
  490. X * FILE: rhparse.c
  491. X * VERSION: 2
  492. X * Written by: Ken Stauffer
  493. X * This contains the parser for the C expressions,
  494. X * gettoken(), getit() and ungetit() routines.
  495. X * sectime(), datespec(), expression(), expr(), exp0(), ... , factor()
  496. X * locatename(), push(), find_macro()
  497. X *
  498. X *
  499. X * ---------------------------------------------------------------------- */
  500. X
  501. X#include "rh.h"
  502. X#include <ctype.h>
  503. X#include <pwd.h>
  504. X
  505. Xstatic int cpos;        /* current character position */
  506. Xstatic int lineno;        /* current line number */
  507. X
  508. X/* ----------------------------------------------------------------------
  509. X * getit:
  510. X *    Return the next character, input is obtained from a file or
  511. X *    a string.
  512. X *    If expstr == NULL then input is from the file called 'expfname'
  513. X *    with file pointer 'expfile'.
  514. X *
  515. X *    If expstr != NULL then input is from the string 'expstr'
  516. X *
  517. X */
  518. X
  519. Xgetit()
  520. X{
  521. X    int c;
  522. X
  523. X    if( expstr ) c = (*expstr) ? *expstr++ : EOF;
  524. X    else c = getc(expfile);
  525. X
  526. X    if( c == '\n' ) { lineno++; cpos = 0; }
  527. X    cpos++;
  528. X
  529. X    return(c);
  530. X}
  531. X
  532. X/* ----------------------------------------------------------------------
  533. X * ungetit:
  534. X *    Unget a char.
  535. X *    A character is ungotten using the same scheme as stated for
  536. X *    getit() for determining where input is comming from.
  537. X *
  538. X */
  539. X
  540. Xungetit(c)
  541. Xint c;
  542. X{
  543. X    if( c == '\n' ) { lineno--; cpos = 1; }
  544. X    else cpos--;
  545. X    if( expstr ) expstr = (c > 0) ? expstr-1 : expstr;
  546. X    else ungetc(c,expfile);
  547. X}
  548. X
  549. X/* ----------------------------------------------------------------------
  550. X * error:
  551. X *    Print an error message and quit.
  552. X */
  553. Xerror(s)
  554. Xchar *s;
  555. X{
  556. X    if( expstr )
  557. X        fprintf(stderr,"Command line: ");
  558. X    else
  559. X        fprintf(stderr,"%s: ",expfname);
  560. X
  561. X    fprintf(stderr,"line: %d, char: %d, %s.\n",lineno,cpos,s);
  562. X    exit(1);
  563. X}
  564. X
  565. X/* ----------------------------------------------------------------------
  566. X * insertname:
  567. X *    Inserts the symbol named 's' with type 't' and value 'val'
  568. X *    into the symbol table. Return the a pointer to the symbol
  569. X *    table entry. The symbol is inserted into the head of the
  570. X *    linked list. This behavior is relied upon elswhere.
  571. X *
  572. X */
  573. X
  574. Xstruct symbol *insertname(s,t,val)
  575. Xchar *s;
  576. Xint t;
  577. Xlong val;
  578. X{
  579. X    char *p,*malloc();
  580. X    struct symbol *sym;
  581. X
  582. X    sym = (struct symbol *) malloc( sizeof(struct symbol) );
  583. X    if( sym == NULL ) error("no more memory");
  584. X
  585. X    p = sym->name = malloc( strlen(s)+1 );
  586. X    if( sym->name == NULL ) error("no more memory");
  587. X    while( *p++ = *s++ );
  588. X    sym->type = t;
  589. X    sym->value = val;
  590. X
  591. X    sym->next = symbols;
  592. X    symbols = sym;
  593. X    
  594. X    return( sym );
  595. X}
  596. X
  597. X/* ----------------------------------------------------------------------
  598. X * locatename:
  599. X *    Do a linear search for 's' in the linked list symbols.
  600. X *
  601. X */
  602. X
  603. Xstruct symbol *locatename(s)
  604. Xchar *s;
  605. X{
  606. X    struct symbol *p;
  607. X
  608. X    for(p=symbols; p; p = p->next )
  609. X        if( !strcmp(s,p->name) ) return(p);
  610. X
  611. X    return(NULL);
  612. X}
  613. X
  614. X/* ----------------------------------------------------------------------
  615. X * push:
  616. X *    "assemble" the instruction into the StackProgram[] array.
  617. X *
  618. X */
  619. X
  620. Xpush(func,val)
  621. Xint (*func)();
  622. Xlong val;
  623. X{
  624. X    if( PC >= LENGTH ) error("program to big");
  625. X    StackProgram[PC].func=func;
  626. X    StackProgram[PC++].value=val;
  627. X}
  628. X
  629. X/* ----------------------------------------------------------------------
  630. X * program:
  631. X *    Parse a program of the form:
  632. X *        <program> ==> <function-list> <expression> EOF
  633. X *            | <function-list> EOF
  634. X *            | <function-list> <expression> ;
  635. X *
  636. X *        <function-list> ==> <function> <function-list>
  637. X *                | empty
  638. X */
  639. X
  640. Xprogram()
  641. X{
  642. X    cpos = 0; lineno = 1;
  643. X
  644. X    token = gettoken();
  645. X    for(;;) {
  646. X        if( token != IDENTIFIER ) break;
  647. X        function();
  648. X    }
  649. X
  650. X    if( token != EOF ) {
  651. X        startPC = PC;
  652. X        expression();
  653. X        push(NULL,0);
  654. X    }
  655. X    if( token != EOF && token != ';') error("EOF expected");
  656. X}
  657. X
  658. X/* ----------------------------------------------------------------------
  659. X * function:
  660. X *    parse a function definition. Grammer for a function is:
  661. X *    <function> ==> IDENTIFIER <id-list> { RETURN <expression> ; }
  662. X *
  663. X *    <id-list> ==> ( <ids> )
  664. X *            | ( )
  665. X *            | empty
  666. X *
  667. X *    <ids> ==> IDENTIFIER <idtail>
  668. X *
  669. X *    <idtail> ==> , <ids>
  670. X *        | empty
  671. X *
  672. X */
  673. X
  674. Xfunction()
  675. X{
  676. X    struct symbol *s;
  677. X
  678. X    s = tokensym;
  679. X    tokensym->value = PC;
  680. X    tokensym->type = FUNCTION;
  681. X    tokensym->func = c_func;
  682. X
  683. X    token = gettoken();
  684. X
  685. X    push(NULL, idlist() );        /* save number of args for function */
  686. X
  687. X    if( token != '{' ) error("expected '{'");
  688. X    token = gettoken();
  689. X
  690. X    if( token != RETURN ) error("expected keyword: return");
  691. X    token = gettoken();
  692. X
  693. X    expression();
  694. X
  695. X    if( token != ';' ) error("expected ';'");
  696. X    token = gettoken();
  697. X
  698. X    push(c_return,StackProgram[s->value].value);
  699. X
  700. X    /* free up the parameter symbols */
  701. X    while( symbols->type == PARAM ) {
  702. X        s = symbols;
  703. X        symbols = symbols->next;
  704. X        free(s->name);
  705. X        free(s);
  706. X    }
  707. X
  708. X    if( token != '}' ) error("expected '}'");
  709. X    token = gettoken();
  710. X}
  711. X
  712. X/* ----------------------------------------------------------------------
  713. X * idlist:
  714. X *    Return the maximum offset obtained in parsing the parameter list.
  715. X *    <id-list> ==> ( <ids> )
  716. X *        | ()
  717. X *        | empty
  718. X *
  719. X *    <ids> ==> IDENTIFIER <idtail>
  720. X *    <idtail> ==> <ids> , <idtail>
  721. X *        | empty
  722. X */
  723. X
  724. Xidlist()
  725. X{
  726. X    int offset = 0;
  727. X
  728. X    if( token == '(' ) token = gettoken();
  729. X    else if( token == '{' ) return(0);
  730. X    else error("expected '(' or '{'");
  731. X
  732. X    if( token == ')' ) {
  733. X        token = gettoken();
  734. X        return(0);
  735. X    }
  736. X
  737. X    for(;;) {
  738. X        if( token != IDENTIFIER ) error("identifier expected");
  739. X        tokensym->type = PARAM;
  740. X        tokensym->func = c_param;
  741. X        tokensym->value = offset++;
  742. X        token = gettoken();
  743. X        if( token == ')' ) break;
  744. X        if( token != ',' ) error("expected ')'");
  745. X        token = gettoken();
  746. X    }
  747. X
  748. X    token = gettoken();
  749. X    return(offset);
  750. X}
  751. X
  752. X/* ----------------------------------------------------------------------
  753. X * expression:
  754. X *    Parse an expression. (top-level routine)
  755. X *    OPERATOR ?:
  756. X *
  757. X */
  758. X
  759. Xexpression()
  760. X{
  761. X    int qm,colon,nop;
  762. X
  763. X    expr0();
  764. X    if( token == '?' ) {
  765. X        token = gettoken();
  766. X        qm = PC;
  767. X        push(c_qm,0);
  768. X        expression();
  769. X        if( token != ':' ) error("missing ':'");
  770. X        token = gettoken();
  771. X        colon = PC;
  772. X        push(c_colon,0);
  773. X        expression();
  774. X
  775. X        StackProgram[qm].value = colon;
  776. X        StackProgram[colon].value = PC-1;
  777. X    }
  778. X}        
  779. X
  780. X/* OPERATOR || */ 
  781. Xexpr0()
  782. X{
  783. X    expr1();
  784. X    for(;;)
  785. X        if( token == OR ) {
  786. X            token = gettoken();
  787. X            expr1();
  788. X            push(c_or,0);
  789. X           } else break;
  790. X}
  791. X
  792. X/* OPERATOR && */ 
  793. Xexpr1()
  794. X{
  795. X    expr2();
  796. X    for(;;)
  797. X        if( token == AND ) {
  798. X            token = gettoken();
  799. X            expr2();
  800. X            push(c_and,0);
  801. X        } else break;
  802. X}
  803. X
  804. X/* OPERATOR | */
  805. Xexpr2()
  806. X{
  807. X    expr3();
  808. X    for(;;)
  809. X        if( token == '|' ) {
  810. X            token = gettoken();
  811. X            expr3();
  812. X            push(c_bor,0);
  813. X        } else break;
  814. X}
  815. X
  816. X/* OPERATOR ^ */
  817. Xexpr3()
  818. X{
  819. X    expr4();
  820. X    for(;;)
  821. X        if( token == '^' ) {
  822. X            token = gettoken();
  823. X            expr4();
  824. X            push(c_bxor,0);
  825. X        } else break;
  826. X}
  827. X
  828. X/* OPERATOR & */
  829. Xexpr4()
  830. X{
  831. X    expr5();
  832. X    for(;;)
  833. X        if( token == '&' ) {
  834. X            token = gettoken();
  835. X            expr5();
  836. X            push(c_band,0);
  837. X        } else break;
  838. X}
  839. X
  840. X/* OPERATOR == != */
  841. Xexpr5()
  842. X{
  843. X    int t;
  844. X    expr6();
  845. X    for(;t=token;)
  846. X        if( t==EQ ) {
  847. X            token = gettoken();
  848. X            expr6();
  849. X            push(c_eq,0);
  850. X        } else if( t==NE ) {
  851. X            token = gettoken();
  852. X            expr6();
  853. X            push(c_ne,0);
  854. X        } else break;
  855. X}
  856. X
  857. X/* OPERATOR < <= > >= */
  858. Xexpr6()
  859. X{
  860. X    int t;
  861. X    expr7();
  862. X    for(;t=token;)
  863. X        if( t==LE ) {
  864. X            token = gettoken();
  865. X            expr7();
  866. X            push(c_le,0);
  867. X        } else if( t==GE ) {
  868. X            token = gettoken();
  869. X            expr7();
  870. X            push(c_ge,0);
  871. X        } else if( t=='>' ) {
  872. X            token = gettoken();
  873. X            expr7();
  874. X            push(c_gt,0);
  875. X        } else if( t=='<' ) {
  876. X            token = gettoken();
  877. X            expr7();
  878. X            push(c_lt,0);
  879. X        } else break;
  880. X}
  881. X
  882. X/* OPERATOR << >> */
  883. Xexpr7()
  884. X{
  885. X    int t;
  886. X    expr8();
  887. X    for(;t=token;)
  888. X        if( t==SHIFTL ) {
  889. X            token = gettoken();
  890. X            expr8();
  891. X            push(c_lshift,0);
  892. X        } else if( t==SHIFTR ) {
  893. X            token = gettoken();
  894. X            expr8();
  895. X            push(c_rshift,0);
  896. X        } else break;
  897. X}
  898. X
  899. X/* OPERATOR + - */
  900. Xexpr8()
  901. X{
  902. X    int t;
  903. X    expr9();
  904. X    for(;t=token;)
  905. X        if( t=='+' ) {
  906. X            token = gettoken();
  907. X            expr9();
  908. X            push(c_plus,0);
  909. X        } else if( t=='-' ) {
  910. X            token = gettoken();
  911. X            expr9();
  912. X            push(c_minus,0);
  913. X        } else break;
  914. X}
  915. X
  916. X/* OPERATOR * / % */
  917. Xexpr9()
  918. X{
  919. X    int t;
  920. X    expr10();
  921. X    for(;t=token;)
  922. X        if( t== '*' ) {
  923. X            token = gettoken();
  924. X            expr10();
  925. X            push(c_mul,0);
  926. X        } else if( t== '/' ) {
  927. X            token = gettoken();
  928. X            expr10();
  929. X            push(c_div,0);
  930. X        } else if( t== '%' ) {
  931. X            token = gettoken();
  932. X            expr10();
  933. X            push(c_mod,0);
  934. X        } else break;
  935. X}
  936. X
  937. X/* OPERATOR ~ ! - */ 
  938. Xexpr10()
  939. X{
  940. X    int t;
  941. X    t = token;    
  942. X    if( t=='!' ){
  943. X        token = gettoken();
  944. X        expr10();
  945. X        push(c_not,0);
  946. X    } else if( t== '~' ) {
  947. X        token = gettoken();
  948. X        expr10();
  949. X        push(c_bnot,0);
  950. X    } else if( t== '-' ) {
  951. X        token = gettoken();
  952. X        expr10();
  953. X        push(c_uniminus,0);
  954. X    } else factor();
  955. X}
  956. X
  957. X/* ----------------------------------------------------------------------
  958. X * explist:
  959. X *    argc is the number of arguments expected.
  960. X *    Parse an expression list of the form:
  961. X *        <explist> ==> ( <exps> )
  962. X *            | ( )
  963. X *            | empty
  964. X *
  965. X *        <exps> ==> <exps> , <expression>
  966. X *            | <expression>
  967. X *
  968. X */
  969. X
  970. Xexplist( argc )
  971. X{
  972. X    if( token != '(' && !argc ) return;
  973. X
  974. X    if( token != '(' ) error("missing '('");
  975. X    token = gettoken();
  976. X
  977. X    if( !argc && token == ')' ) {
  978. X        token = gettoken();
  979. X        return;
  980. X    }
  981. X
  982. X    for(;;) {
  983. X        expression();
  984. X        argc--;
  985. X        if( token == ')' ) break;
  986. X        if( token != ',' ) error("missing ','");
  987. X        token = gettoken();
  988. X    }
  989. X
  990. X    token = gettoken();
  991. X    if( argc ) error("wrong number of arguments");
  992. X}    
  993. X
  994. X/* ----------------------------------------------------------------------
  995. X * factor:
  996. X *    Parse a factor. Could be a number, variable, date, function call or
  997. X *    regular expression string.
  998. X */
  999. Xfactor()
  1000. X{
  1001. X    long l,datespec();
  1002. X    int pc;
  1003. X
  1004. X    switch(token) {
  1005. X        case '(':
  1006. X            token = gettoken();
  1007. X            expression();
  1008. X            if( token != ')' )
  1009. X                error("missing ')'");
  1010. X            token = gettoken();
  1011. X            break;
  1012. X        case NUMBER:
  1013. X            push(c_number,tokenval);
  1014. X            token = gettoken();
  1015. X            break;
  1016. X        case FUNCTION:
  1017. X            pc = tokensym->value;
  1018. X            token = gettoken();
  1019. X            explist( StackProgram[ pc ].value );
  1020. X            push(c_func,pc);
  1021. X            break;
  1022. X        case PARAM:
  1023. X            push(c_param,tokensym->value);
  1024. X            token = gettoken();
  1025. X            break;
  1026. X        case FIELD:
  1027. X            push(tokensym->func,tokenval);
  1028. X            token = gettoken();
  1029. X            break;
  1030. X        case '[':
  1031. X            token = gettoken();
  1032. X            l=datespec();
  1033. X            if( token != ']' )
  1034. X                error("missing ']'");
  1035. X            token = gettoken();
  1036. X            push(c_number,l);
  1037. X            break;
  1038. X        case STR:
  1039. X            push(c_str,tokenval);
  1040. X            token = gettoken();
  1041. X            break;
  1042. X        case IDENTIFIER:
  1043. X            error("undefined identifier");
  1044. X        default:
  1045. X            error("syntax error");
  1046. X    }
  1047. X}
  1048. X
  1049. X/* ----------------------------------------------------------------------
  1050. X * sectime:
  1051. X *    calculate the number of seconds between January 1, 1970
  1052. X *    and year/month/day. Return that value.
  1053. X *
  1054. X */
  1055. X
  1056. X#define leap(d)    (((d % 4 == 0) && (d % 100 != 0)) || (d % 400 == 0))
  1057. X#define DAYSEC    (3600*24)
  1058. X#define YERSEC    (3600*24*365)
  1059. X#define TIME0    1970
  1060. X
  1061. Xlong sectime(year,month,day)
  1062. Xint year,month,day;
  1063. X{
  1064. X
  1065. X        static int months[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
  1066. X    int yeardays,leapers,x;
  1067. X    long seconds;
  1068. X
  1069. X    if(month>12 || month<1 || year<TIME0 || day<1 || day>months[month]+
  1070. X        (month==2 && leap(year)) )
  1071. X            return(-1);
  1072. X
  1073. X    yeardays = leapers = 0;
  1074. X
  1075. X    for(x=1;x<month;x++)
  1076. X        yeardays += months[x];
  1077. X    if ((month > 2) && leap(year)) yeardays++;
  1078. X
  1079. X    for(x=TIME0; x<year; x++)
  1080. X        if(leap(x)) leapers++;
  1081. X    
  1082. X    seconds = yeardays*DAYSEC+(year-TIME0)*YERSEC+7*3600+
  1083. X            leapers*DAYSEC + day*DAYSEC;
  1084. X
  1085. X    return(seconds);
  1086. X
  1087. X}
  1088. X
  1089. X/* ----------------------------------------------------------------------
  1090. X * datespec:
  1091. X *    parse a date. Return the number of seconds from
  1092. X *    some date in 1970, until the specified date.
  1093. X */
  1094. X
  1095. Xlong datespec()
  1096. X{
  1097. X    int year,month,day,seconds;
  1098. X
  1099. X    if( token != NUMBER ) error("number expected");
  1100. X    year = tokenval;
  1101. X    token = gettoken();
  1102. X    if( token != '/' ) error("missing '/'");
  1103. X    token = gettoken();
  1104. X    if( token != NUMBER ) error("number expected");
  1105. X    month = tokenval;
  1106. X    token = gettoken();
  1107. X    if( token != '/' ) error("missing '/'");
  1108. X    token = gettoken();
  1109. X    if( token != NUMBER ) error("number expected");
  1110. X    day = tokenval;
  1111. X    token = gettoken();
  1112. X
  1113. X    if( (seconds = sectime(year,month,day)) < 0 ) 
  1114. X        error("invalid date");
  1115. X
  1116. X    return(seconds);
  1117. X}
  1118. X
  1119. X
  1120. X/* ----------------------------------------------------------------------
  1121. X * gettoken:
  1122. X *    Return the next token.
  1123. X *    global variable: tokenval will contain any extra
  1124. X *    attribute associated with the returned token, ie
  1125. X *    the VALUE of a number, the index of the string etc...
  1126. X *    tokensym will be a pointer to the symbol table entry for
  1127. X *    any symbol encountered.
  1128. X *
  1129. X */
  1130. Xgettoken()
  1131. X{
  1132. X    char buf[IDLENGTH+1],*bufp=buf;
  1133. X    int c,incomment;
  1134. X
  1135. X    incomment = 0;
  1136. X    c = getit();
  1137. X    while( c == ' ' || c == '\t' || c == '\n' || c == '/' || incomment) {
  1138. X       if( c == '/' && !incomment) {
  1139. X        c = getit();
  1140. X        if( c != '*' ) {
  1141. X            ungetit(c);
  1142. X            c = '/';
  1143. X            break;
  1144. X        }
  1145. X        incomment = 1;
  1146. X       } else if( c == '*' ) {
  1147. X        c = getit();
  1148. X        if( c == '/' ) incomment = 0;
  1149. X       }
  1150. X       c = getit();
  1151. X    }
  1152. X
  1153. X    if(c=='0') {
  1154. X        tokenval=0;
  1155. X        while( ( c=getit() ) >= '0' && c <= '7' ) {
  1156. X            tokenval <<= 3;
  1157. X            tokenval += c-'0';
  1158. X        }
  1159. X        if( isdigit(c) ) error("bad octal constant");
  1160. X        ungetit(c);
  1161. X        return(NUMBER);
  1162. X    }
  1163. X    if(isdigit(c)) {
  1164. X        tokenval=c-'0';
  1165. X        while(isdigit( (c=getit()) )) {
  1166. X            tokenval *=10;
  1167. X            tokenval += c-'0';
  1168. X        }
  1169. X        ungetit(c);
  1170. X        return(NUMBER);
  1171. X    }
  1172. X    
  1173. X    if(isalpha(c)) {
  1174. X       int count=0;
  1175. X       do {
  1176. X        if(count++ < IDLENGTH) *bufp++ = c;
  1177. X        c=getit();
  1178. X       } while( isalnum(c) );
  1179. X       ungetit(c);
  1180. X       *bufp='\0';
  1181. X       if( (tokensym=locatename(buf)) == NULL ) {
  1182. X            tokensym = insertname(buf,IDENTIFIER,0);
  1183. X       }
  1184. X       tokenval = tokensym->value;
  1185. X       return( tokensym->type );
  1186. X    }
  1187. X
  1188. X    if( c == '"' ) {
  1189. X        tokenval=strfree;
  1190. X        while( (c=getit()) != '"' ) {
  1191. X            if( strfree > STRLEN )
  1192. X                error("no more string space");
  1193. X            Strbuf[strfree++]= c;
  1194. X        }
  1195. X        Strbuf[strfree++]='\0';
  1196. X        return(STR);
  1197. X    }
  1198. X
  1199. X    if( c == '=' ) {
  1200. X        c=getit();
  1201. X        if(c== '=') return(EQ);
  1202. X        else {
  1203. X            ungetit(c);
  1204. X            return('=');
  1205. X        }
  1206. X    }
  1207. X
  1208. X    if( c== '$' ) {
  1209. X       int count=0;
  1210. X       struct passwd *info,*getpwnam();
  1211. X       c=getit();
  1212. X       if( c=='$' ) {
  1213. X        tokenval = getuid();
  1214. X        return( NUMBER );
  1215. X       }
  1216. X       do {
  1217. X        if (count++ < IDLENGTH) *bufp++ = c;
  1218. X        c=getit();
  1219. X       } while( isalnum(c) );
  1220. X       ungetit(c);
  1221. X       *bufp='\0';
  1222. X       if( (info=getpwnam(buf)) == NULL ) 
  1223. X        error("no such user");
  1224. X       tokenval = info->pw_uid;
  1225. X       return( NUMBER );
  1226. X    }
  1227. X    
  1228. X    if( c == '!' ) {
  1229. X        c=getit();
  1230. X        if( c == '=' ) return(NE);
  1231. X        ungetit(c);
  1232. X        return('!');
  1233. X    }
  1234. X    if( c == '>' ) {
  1235. X        c=getit();
  1236. X        if( c == '=' ) return(GE);
  1237. X        if( c == '>' ) return(SHIFTR);
  1238. X        ungetit(c);
  1239. X        return('>');
  1240. X    }
  1241. X
  1242. X    if( c == '<' ) {
  1243. X        c=getit();
  1244. X        if( c == '=' ) return(LE);
  1245. X        if( c == '<' ) return(SHIFTL);
  1246. X        ungetit(c);
  1247. X        return('<');
  1248. X    }
  1249. X
  1250. X    if( c == '&' ) {
  1251. X        c=getit();
  1252. X        if( c == '&' ) return(AND);
  1253. X        ungetit(c);
  1254. X        return('&');
  1255. X    }
  1256. X
  1257. X    if( c == '|' ) {
  1258. X        c=getit();
  1259. X        if( c == '|' ) return(OR);
  1260. X        ungetit(c);
  1261. X        return('|');
  1262. X    }
  1263. X
  1264. X    return(c);
  1265. X}
  1266. X
  1267. END_OF_FILE
  1268. if test 14503 -ne `wc -c <'rhparse.c'`; then
  1269.     echo shar: \"'rhparse.c'\" unpacked with wrong size!
  1270. fi
  1271. # end of 'rhparse.c'
  1272. fi
  1273. echo shar: End of archive 2 \(of 2\).
  1274. cp /dev/null ark2isdone
  1275. MISSING=""
  1276. for I in 1 2 ; do
  1277.     if test ! -f ark${I}isdone ; then
  1278.     MISSING="${MISSING} ${I}"
  1279.     fi
  1280. done
  1281. if test "${MISSING}" = "" ; then
  1282.     echo You have unpacked both archives.
  1283.     rm -f ark[1-9]isdone
  1284. else
  1285.     echo You still need to unpack the following archives:
  1286.     echo "        " ${MISSING}
  1287. fi
  1288. ##  End of shell archive.
  1289. exit 0
  1290.